package org.boulderfoodrescue.webapp.client.old.calendar.ical;

import org.boulderfoodrescue.webapp.shared.EventInfo;

import com.bradrydzewski.gwt.calendar.client.Appointment;
import com.bradrydzewski.gwt.calendar.client.AppointmentStyle;
import com.bradrydzewski.gwt.calendar.client.Calendar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

public class NewEventPanel extends Composite{

  private NewEventPanel newEventPanel;
  protected boolean warned1 = false;

  private static NewEventPanelUiBinder uiBinder = GWT
      .create(NewEventPanelUiBinder.class);

  interface NewEventPanelUiBinder extends UiBinder<Widget, NewEventPanel> {
  }

  protected DialogBox dlg;
  public NewEventPanel(DialogBox dlg, Calendar calendar, Appointment appt) {
    this.appt = appt;
    this.dlg = dlg;
    this.calendar = calendar;
    initWidget(uiBinder.createAndBindUi(this));
    from_date.setValue(appt.getStart());
    to_date.setValue(appt.getEnd());
  }

  @UiField
  TextBox event_name;
  @UiField
  DateBox from_date;
  @UiField
  DateBox to_date;
  @UiField
  CheckBox repeat;
  @UiField
  ListBox reminder_type;
  @UiField
  ListBox event_color;
  @UiField
  Button button_add_event;
  @UiField
  Button button_cancel_event;
  Calendar calendar;
  Appointment appt;

  protected boolean warned = false; 
  
  @UiHandler("button_add_event")
  public void onSubmitClick(ClickEvent add){
    EventInfo newEvent = new EventInfo();
    newEvent.setEventName(event_name.getText());
    newEvent.setFromDate(from_date.getValue());  
    newEvent.setToDate(to_date.getValue());
    newEvent.setReminderType(reminder_type.getTitle());
    newEvent.setEventColor(event_color.getTitle());
    EventInfoServiceAsync EventInfoService = GWT.create(EventInfoService.class);

    AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>(){
      @Override
      public void onFailure(Throwable caught) {
        Window.alert(caught.getMessage());
      }
      @Override
      public void onSuccess(Boolean result) {
        if (!result){
          Window.alert("Error: saving");
        }
        else
          History.newItem("viewer", true);
      }
    };
    EventInfoService.saveEventData(newEvent, callback);
    //Adding new appointment
    calendar.suspendLayout();
    appt.setTitle(newEvent.getEventName());
    appt.setStart(newEvent.getFromDate());
    appt.setEnd(newEvent.getToDate());
    
    System.out.println(newEvent.getEventColor());
    
    if (newEvent.getEventColor() == "blue"){

      appt.setStyle(AppointmentStyle.BLUE);
    }
    else if (newEvent.getEventColor() == "green"){

      appt.setStyle(AppointmentStyle.GREEN);
    }
    else if (newEvent.getEventColor() == "orange"){

      appt.setStyle(AppointmentStyle.ORANGE);
    }
    else if (newEvent.getEventColor() == "purple"){

      appt.setStyle(AppointmentStyle.PURPLE);
    }
    else if (newEvent.getEventColor() == "red"){

      appt.setStyle(AppointmentStyle.RED);
    }
    else if (newEvent.getEventColor() == "yellow"){

      appt.setStyle(AppointmentStyle.YELLOW);
    }
    calendar.addAppointment(appt);
    calendar.resumeLayout();
    dlg.hide();
  }

  //Open up the RepeatNewEventDialog upon checking the Repeat checkbox
  @UiHandler("repeat")
  public void onCheckedOpenRepeat(ClickEvent e){
    boolean checked = ((CheckBox) e.getSource()).getValue();
    if (checked == true)
    {
      RepeatNewEventDialog eDialog = new RepeatNewEventDialog(newEventPanel);
      eDialog.center();
    }
  }


  @UiHandler("button_cancel_event")
  public void onButtonCancel(ClickEvent e){
    dlg.hide();
  }
  /*

  public void loadEventInfoData(){
    EventInfoServiceAsync eventInfoService = GWT.create(EventInfoService.class);
    AsyncCallback<EventInfo> callback = new AsyncCallback<EventInfo>(){

      @Override
      public void onFailure(Throwable caught) {
        Window.alert(caught.getMessage());

      }

      @Override
      public void onSuccess(EventInfo event) {
        if (event == null){
          return;
        }
        event_name.setText(event.getEventName());
        from_date.setValue(event.getFromDate());
        from_time.setTitle(event.getFromTime());        
        to_date.setValue(event.getToDate());
        to_time.setTitle(event.getToTime());
        event_color.setSelectedIndex(event.getEventColor());
        reminder_type.setTitle(event.getReminderType());      
      }
    };
    EventInfoService.getEvent(callback);
  }
   */
}
